home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / SunImagePlugin.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  1.7 KB  |  85 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: SunImagePlugin.py,v 1.1.1.2 1999/01/13 09:40:00 sjoerd Exp $
  4. #
  5. # Sun image file handling
  6. #
  7. # History:
  8. # 95-09-10 fl    Created
  9. # 96-05-28 fl    Fixed 32-bit alignment
  10. # 98-12-29 fl    Import ImagePalette module
  11. #
  12. # Copyright (c) Secret Labs AB 1997-98.
  13. # Copyright (c) Fredrik Lundh 1995-96.
  14. #
  15. # See the README file for information on usage and redistribution.
  16. #
  17.  
  18.  
  19. __version__ = "0.2"
  20.  
  21.  
  22. import regex, string
  23. import Image, ImageFile, ImagePalette
  24.  
  25.  
  26. def i16(c):
  27.     return ord(c[1]) + (ord(c[0])<<8)
  28.  
  29. def i32(c):
  30.     return ord(c[3]) + (ord(c[2])<<8) + (ord(c[1])<<16) + (ord(c[0])<<24)
  31.  
  32.  
  33. def _accept(prefix):
  34.     return i32(prefix) == 0x59a66a95
  35.  
  36.  
  37. class SunImageFile(ImageFile.ImageFile):
  38.  
  39.     format = "SUN"
  40.     format_description = "Sun Raster File"
  41.  
  42.     def _open(self):
  43.  
  44.     # HEAD
  45.     s = self.fp.read(32)
  46.     if i32(s) != 0x59a66a95:
  47.         raise SyntaxError, "not an SUN raster file"
  48.  
  49.     offset = 32
  50.  
  51.     self.size = i32(s[4:8]), i32(s[8:12])
  52.  
  53.     depth = i32(s[12:16])
  54.     if depth == 1:
  55.         self.mode, rawmode = "1", "1;I"
  56.     elif depth == 8:
  57.         self.mode = rawmode = "L"
  58.     elif depth == 24:
  59.         self.mode, rawmode = "RGB", "BGR"
  60.     else:
  61.         raise SyntaxError, "unsupported mode"
  62.  
  63.     compression = i32(s[20:24])
  64.  
  65.     if i32(s[24:28]) != 0:
  66.         length = i32(s[28:32])
  67.         offset = offset + length
  68.         self.palette = ImagePalette.raw("RGB;L", self.fp.read(length))
  69.         if self.mode == "L":
  70.         self.mode = "P"
  71.  
  72.     stride = (((self.size[0] * depth + 7) / 8) + 3) & (~3)
  73.  
  74.     if compression == 1:
  75.         self.tile = [("raw", (0,0)+self.size, offset, (rawmode, stride))]
  76.     elif compression == 2:
  77.         self.tile = [("sun_rle", (0,0)+self.size, offset, rawmode)]
  78.  
  79. #
  80. # registry
  81.  
  82. Image.register_open("SUN", SunImageFile, _accept)
  83.  
  84. Image.register_extension("SUN", ".ras")
  85.